writeLines("************************************************************************************************************************")
writeLines("************************************************************************************************************************")

# 	Filename: overimputationplot_source.R
# 	Description: Source script to perform overimputation plot PDF.
writeLines("Description: Source script to create a PDF of the overimputation plot from the Supplementary Appendix")
# 	Author: Barry Hashimoto
# 	Date: December 2019
# 	For: Barry Hashimoto, "Autocratic Consent to International Law: the Case of the International Criminal Court's Jurisdiction," International Organization.

###############################################################################################################################
# Single graph of overimputation plots: (draws x nimps) simulations per data point per graph, 90% confidence intervals.
# Imputations along the 45 degree line are what we want, especially when the color of the vert. lines is not blue.

timestamp()
set.seed(100)

rm(list = ls())
gc()

suppressPackageStartupMessages({library(Amelia)})
load("MIforPlot.Rdata")

writeLines(""); writeLines("")
writeLines("----------------------------------------------------------------------------------------------------------------------")
writeLines("Part II: Produce overimputation plots to verify imputation accuracy. Computationally intensive!")

draws <- 50 # simulations per data point per imputation. nimps*draws = total number of simulations per data point.

pdf("overimputationplot.pdf", width=12, height=12)
par(mfrow = c(4, 4))
par(cex = 0.85, cex.main = 1.05, cex.sub = 0.8, cex.lab = 0.95)
par(mar = c(2.5,2.5,1,0), oma = c(1.1,1,1,1))
 overimpute(full, var = "capital.scaled", draws = draws, main = "capital.scaled")
 overimpute(full, var = "gdp.scaled", draws = draws, main = "gdp.scaled") 
 overimpute(full, var = "loggrowth", draws = draws, main = "loggrowth") 
 overimpute(full, var = "logoilrent", draws = draws, main = "logoilrent") 
 overimpute(full, var = "allunemp", draws = draws, main ="allunemp") 
 overimpute(full, var = "maleunemp", draws = draws, main ="maleunemp") 
 overimpute(full, var = "vdem.ruleoflaw", draws = draws, main = "vdem.ruleoflaw") 
 overimpute(full, var = "polyarchy", draws = draws, main = "polyarchy (V-Dem)")
 overimpute(full, var = "ptssum.mean", draws = draws, main = "ptssum.mean")
 overimpute(full, var = "eth", draws = draws, main = "eth") 
 overimpute(full, var = "rel", draws = draws, main = "rel") 
 overimpute(full, var = "logexpmil", draws = draws, main = "logexpmil") 
 overimpute(full, var = "logsoldiers", draws = draws, main = "logsoldiers") 
 overimpute(full, var = "logsoldierspercent", draws = draws, main = "logsoldierspercent") 
 overimpute(full, var = "logrefugees", draws = draws, main = "logrefugees") 
 overimpute(full, var = "logpop", draws = draws, main = "logpop")
dev.off()
	
writeLines("Plot saved as overimputationplot.pdf in your working directory.")
timestamp()
